﻿using System;

using System.Collections.Generic;
using System.Text;

namespace DirectUI.Mobile.UI
{
    public enum DataSourceState
    {
        Waiting=1,
        Loading=2,
        Error=4,
        Ready=3
    }
    // Summary:
    //     Common base class and contract for System.Windows.Data.DataSourceProvider
    //     objects, which are factories that execute some queries to produce a single
    //     object or a list of objects that you can use as binding source objects.
    public abstract class DataSourceProvider
    {
        // Summary:
        //     Initializes a new instance of the System.Windows.Data.DataSourceProvider
        //     class. This is a protected constructor.
        protected DataSourceProvider()
        {
        }
        protected object _data;
        private DataSourceState _state = DataSourceState.Waiting;
        public DataSourceState State
        {
            get
            {
                return _state;
            }
            set
            {
                _state = value;
                OnDataChanged(null);
            }
        }
        public string LastError { get; set; }
        public object Data
        {
            get
            {
                if (_data == null)
                {
                    BeginQuery();
                }
                return _data;
            }
        }
        private bool _autoLoad = false;
        public bool AutoLoad
        {
            get
            {
                return _autoLoad;
            }
            set
            {
                _autoLoad = value;
            }
        }
        private delegate void OnDataChangedDelegate(EventArgs e);
        public event EventHandler DataChanged;
        protected void OnDataChanged( EventArgs e)
        {
            if (DirectUI.Mobile.UI.Controls.DirectUIHost.Instance != null)
            {
                if (DirectUI.Mobile.UI.Controls.DirectUIHost.Instance.InvokeRequired)
                {
                    DirectUI.Mobile.UI.Controls.DirectUIHost.Instance.Invoke(new OnDataChangedDelegate(OnDataChanged), new object[] { e });
                    return;
                } 
            }
            if (DataChanged != null)
            {
                DataChanged(this, e);
            }
        }
        protected System.Exception _error;
        public System.Exception Error
        {
            get
            {
                return _error;
            }
        }
        //
        // Summary:
        //     When overridden in a derived class, this base class calls this method when
        //     System.Windows.Data.DataSourceProvider.InitialLoad() or System.Windows.Data.DataSourceProvider.Refresh()
        //     has been called. The base class delays the call if refresh is deferred or
        //     initial load is disabled.
        protected virtual void BeginQuery()
        {
        }
        //
        // Summary:
        //     Initiates a refresh operation to the underlying data model. The result is
        //     returned on the System.Windows.Data.DataSourceProvider.Data property.
        public virtual void Refresh()
        {
            BeginQuery();
        }
    }
}
